import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class GradeManagementSystem {
    private List<Student> students;  // 存储学生对象的列表
    private int courseCount;  // 课程数量

    // 构造方法，初始化成绩管理系统
    public GradeManagementSystem(int courseCount) {
        this.students = new ArrayList<>();
        this.courseCount = courseCount;
    }

    // 添加学生信息
    public void addStudent(String id, String name) {
        Student student = new Student(id, name, courseCount);
        students.add(student);
    }

    // 录入学生成绩
    public void inputScores() {
        Scanner scanner = new Scanner(System.in);
        for (Student student : students) {
            System.out.println("请输入 " + student.getName() + " 的成绩：");
            for (int i = 0; i < courseCount; i++) {
                System.out.print("第 " + (i + 1) + " 门课程成绩：");
                double score = scanner.nextDouble();
                student.setScore(i, score);
            }
        }
    }

    // 查询学生成绩
    public void queryScores() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要查询的学生学号：");
        String id = scanner.nextLine();
        for (Student student : students) {
            if (student.getId().equals(id)) {
                System.out.println("学生姓名：" + student.getName());
                System.out.print("成绩：");
                for (double score : student.getScores()) {
                    System.out.print(score + " ");
                }
                System.out.println();
                System.out.println("平均成绩：" + student.getAverageScore());
                return;
            }
        }
        System.out.println("未找到该学生信息。");
    }

    // 统计所有学生的平均成绩
    public void statistics() {
        if (students.isEmpty()) {
            System.out.println("暂无学生信息。");
            return;
        }
        double totalAverage = 0;
        for (Student student : students) {
            totalAverage += student.getAverageScore();
        }
        System.out.println("所有学生的平均成绩：" + totalAverage / students.size());
    }

    // 显示菜单
    public void showMenu() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("=== 学生成绩管理系统 ===");
            System.out.println("1. 添加学生");
            System.out.println("2. 录入成绩");
            System.out.println("3. 查询成绩");
            System.out.println("4. 统计平均成绩");
            System.out.println("5. 退出");
            System.out.print("请输入你的选择：");
            int choice = scanner.nextInt();
            scanner.nextLine();  // 消耗掉换行符

            switch (choice) {
                case 1:
                    System.out.print("请输入学生学号：");
                    String id = scanner.nextLine();
                    System.out.print("请输入学生姓名：");
                    String name = scanner.nextLine();
                    addStudent(id, name);
                    System.out.println("学生添加成功。");
                    break;
                case 2:
                    inputScores();
                    System.out.println("成绩录入成功。");
                    break;
                case 3:
                    queryScores();
                    break;
                case 4:
                    statistics();
                    break;
                case 5:
                    System.out.println("感谢使用，再见！");
                    return;
                default:
                    System.out.println("无效的选择，请重新输入。");
            }
        }
    }

}
